package com.dtda.edu.miaosha.controller;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.dtda.edu.miaosha.bo.ItemBo;
import com.dtda.edu.miaosha.bo.MarketBo;
import com.dtda.edu.miaosha.common.exception.BaseExcpController;
import com.dtda.edu.miaosha.common.exception.BusinessException;
import com.dtda.edu.miaosha.common.response.EmBusinessError;
import com.dtda.edu.miaosha.common.response.ResponseType;
import com.dtda.edu.miaosha.service.ActivityService;
import com.dtda.edu.miaosha.service.ItemService;

/**
 * @author 袁强
 * @version 2019年3月25日 下午2:27:04
 * 类说明  
 */
@RestController
@RequestMapping("/")
public class ActivityController extends BaseExcpController{

	@Autowired
	private ActivityService activityService;
	@Autowired
	private ItemService itemService;
	
	@RequestMapping("/selActivity")
	public ModelAndView selActivity() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/activity/activity");
		return modelAndView;
	}
	@RequestMapping("/showActivity")
	public ResponseType showActivity() throws BusinessException {
		List<ItemBo> list = activityService.showActivities();
		if(list == null) {
			throw new BusinessException(EmBusinessError.ACTIVITY_NOT_EXIST);
		}
		return ResponseType.create(list);    //bootstrap table 会根据返回结果自动进行校验
		/*if(list.size()>0) {
			return ResponseType.create(list);
		}else {
			return ResponseType.create("fail", "当前没有活动!");
		}*/
	}
	@RequestMapping("/showActByItemId")
	public ResponseType showActByItemId(@RequestParam(name="itemId")String itemId) throws BusinessException {
		ItemBo itemBo = activityService.showActivityByItemId(itemId);
		if(itemBo!=null) {
			return ResponseType.create(itemBo);
		}else {
			return ResponseType.create("fail", "查询异常...");
		}
	}
	
	@RequestMapping("/addActivity")
	public ResponseType addActivity(@RequestParam(name="marketName")String marketName,
									@RequestParam(name="titleId")String titleId,
									@RequestParam(name="marketItemPrice")String marketItemPrice,
									@RequestParam(name="startDate")String startDate,
									@RequestParam(name="endDate")String endDate) throws Exception {
		if(marketName=="" || marketName ==null) {
			throw new BusinessException(EmBusinessError.PARAM_VALIDATION_ERROR,"活动名称不能为空");
		}
		if(titleId=="" || titleId ==null) {
			throw new BusinessException(EmBusinessError.PARAM_VALIDATION_ERROR,"商品名不能为空");
		}
		if(marketItemPrice=="" || marketItemPrice ==null) {
			throw new BusinessException(EmBusinessError.PARAM_VALIDATION_ERROR,"活动价格不能为空");
		}
		if(startDate=="" || startDate ==null) {
			throw new BusinessException(EmBusinessError.PARAM_VALIDATION_ERROR,"活动开始时间不能为空");
		}
		if(endDate=="" || endDate ==null) {
			throw new BusinessException(EmBusinessError.PARAM_VALIDATION_ERROR,"活动结束时间不能为空");
		}
		MarketBo marketBo = new MarketBo();
		marketBo.setMarketName(marketName);
		marketBo.setItemId(Integer.valueOf(titleId));
		marketBo.setMarketItemPrice(BigDecimal.valueOf(Double.parseDouble(marketItemPrice)));
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
		startDate = startDate.replace(" ", "");
		endDate=endDate.replace(" ", "");
		marketBo.setStartDate(dateFormat.parse(startDate));
		marketBo.setEndDate(dateFormat.parse(endDate));
		int i = activityService.insertActivity(marketBo);
		if(i > 0) {
			return ResponseType.create("活动添加成功!");
		}else {
			return ResponseType.create("fail","活动添加失败!");
		}
	}
	
	@RequestMapping("/goAddActivity")
	public ModelAndView goAddActivity() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("itemList", itemService.showItems());
		modelAndView.setViewName("/activity/addActivity");
		return modelAndView;
	}
	
	@RequestMapping("/updateActivity")
	@ResponseBody
	public ModelAndView updateActivity(@RequestParam(name="itemId")String itemId){
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("activityItemId", itemId);
		modelAndView.setViewName("/activity/activityDetail");
		return modelAndView;
	}
	@RequestMapping("/updActivity")
	@ResponseBody
	public ResponseType updActivity(
			@RequestParam(name="activityId")String activityId,
			   @RequestParam(name="marketName")String marketName,
			   @RequestParam(name="marketItemPrice")String marketItemPrice,
			   @RequestParam(name="startDate")String startDate,
			   @RequestParam(name="endDate")String endDate) throws Exception {
		MarketBo marketBo = new MarketBo();
		marketBo.setId(Integer.valueOf(activityId));
		marketBo.setMarketName(marketName);
		marketBo.setMarketItemPrice(BigDecimal.valueOf(Double.parseDouble(marketItemPrice)));
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");
		startDate = startDate.replace(" ", "");
		endDate=endDate.replace(" ", "");
		marketBo.setStartDate(dateFormat.parse(startDate));
		marketBo.setEndDate(dateFormat.parse(endDate));
		boolean b= activityService.updateActivity(marketBo);
		if(! b) {
			return ResponseType.create("fail", "修改商品失败!");
		}else {
			return ResponseType.create("修改商品成功!");
		}
	}
	@RequestMapping("/delActivity")
	@ResponseBody
	public ResponseType delActivity(@RequestParam(name="activityId")String activityId) {
		boolean b = activityService.deleteActivity(Integer.valueOf(activityId));
		if(! b) {
			return ResponseType.create("fail", "删除活动信息失败!");
		}
		return ResponseType.create("删除活动信息成功!");
	}
}
